<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('库存列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>条码：<input type="text" name="barcode" oninput="codeChange(this)"/></li>
                        <li>饰品名称：<input type="text" name="name"/></li>
                        <li>门店：<input class="" type="text" onclick="selectDeptTree()" id="treeName" readonly="true"><input name="deptId" style="display: none;" id="treeId" th:value="${deptId}"/></li>
                        <li>品类：
                            <select name="category" th:with="type=${@level.getType('100')}">
                                <option value="">所有</option>
                                <option th:each="level : ${type}" th:text="${level.typeName}" th:value="${level.typeId}"></option>
                            </select>
                        </li>
                        <li>品种：
                            <select name="variety"><option value="">所有</option></select>
                        </li>
                        <li class="select-time">
                            <label>入库时间： </label>
                            <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()" id="searchId"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:tInventory:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()"
               shiro:hasPermission="system:tInventory:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="system:tInventory:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('system:tInventory:edit')}]];
    var removeFlag = [[${@permission.hasPermi('system:tInventory:remove')}]];
    var prefix = ctx + "system/tInventory";

    $("select[name='category']").change(function () {
        var id = $("select[name='category']").val();
        getLevel2(id);
    });

    function getLevel2(id) {
        $.ajax({
            type: "get",
            url: ctx + 'system/types/getLevel2/' + id,
            dataType: "json",
            cache: false, //关闭AJAX缓存
            data: null,
            success: function (data) {
                var levelList = data.data;
                $("select[name='variety']").empty();
                $("select[name='variety']").append("<option value=''>选择二级</option>");
                $.each(levelList, function (i, d) {
                    $("select[name='variety']").append("<option value='" + d.typeId + "'>" + d.typeName + "</option>");
                });
            }
        });
    }

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "库存",
            sortName: "indate",
            sortOrder: "desc",
            showFooter: true,
            showExport: true,
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '序号',
                    visible: false
                },
                {
                    field: 'barcode',
                    title: '条形码',
                    visible: true
                },
                {
                    field: 'name',
                    title: '饰品名称',
                    sortable: false
                },

                {
                    field: 'deptName',
                    title: '门店'
                },
                {
                    field: 'categoryName',
                    title: '品类',
                    sortable: false
                },
                {
                    field: 'varietyName',
                    title: '品种',
                    sortable: false
                },
                {
                    field: 'weight',
                    title: '重量(g)',
                    sortable: false,
                    footerFormatter:function (value) {
                        var sumBalance = 0;
                        for (var i in value) {
                            sumBalance += parseFloat(value[i].weight);
                        }
                        return sumBalance.toFixed(2);
                    }
                },
                {
                    field: 'labelprice',
                    title: '标签价格',
                    sortable: false,
                    footerFormatter:function (value) {
                        var sumBalance = 0;
                        for (var i in value) {
                            sumBalance += parseFloat(value[i].labelprice);
                        }
                        return sumBalance.toFixed(2);
                    }
                },
                {
                    field: 'indate',
                    title: '入库时间',
                    sortable: false
                },
                {
                    field: 'registrant',
                    title: '登记者',
                    sortable: false
                },
                {
                    field: 'issale',
                    title: '是否销售',
                    sortable: false,
                    formatter: function (value, row, index) {
                        if(row.issale == '0'){
                            return "<span style=\"color: red\">未销售</span>";
                        }else{
                            return "<span style=\"color: #f8ac59\">已销售</span>";
                        }
                    }
                },
                {
                    field: 'isstocktaking',
                    title: '是否盘点',
                    sortable: false,
                    formatter: function (value, row, index) {
                        if(row.isstocktaking == '0'){
                            return "<span style=\"color: red\">未盘点</span>";
                        }else{
                            return "<span style=\"color: #23c6c8\">已盘点</span>";
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        if(row.issale == '0'){
                            actions.push('<a class="btn btn-warning btn-warning btn-xs ' + editFlag + '" href="#" onclick="sale(\'' + row.id + '\')"><i class="fa fa-edit"></i>销售</a> ');
                        }
                        if(row.isstocktaking == '0'){
                            actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="#" onclick="stocktaking(\'' + row.id + '\')"><i class="fa fa-edit"></i>盘点</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    
    /*用户管理-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = $.common.isEmpty(treeId) ? "100" : $("#treeId").val();
        var url = ctx + "system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: ctx + "system/dept/selectDeptTree/" + deptId,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }
    
    function codeChange(obj) {
        var code = $(obj).val()
        $("#searchId").trigger("click");
    }
    function sale(id) {
        var title = "销售";
        var url = prefix + "/sale/"+id;
        var width,height,callback;
        //如果是移动端，就使用自适应大小弹窗
        if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {width = 'auto';height = 'auto';}
        if ($.common.isEmpty(title)) {title = false;};
        if ($.common.isEmpty(url)) {url = "/404.html";};
        if ($.common.isEmpty(width)) {width = 800;};
        if ($.common.isEmpty(height)) {height = ($(window).height() - 50);};
        if ($.common.isEmpty(callback)) {
            callback = function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler();
            }
        }
        layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['确定销售', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: callback,
            cancel: function(index) {
                return true;
            }
        });
    }
    function stocktaking(id) {
        var title = "盘点";
        var url = prefix + "/stocktaking/"+id;
        var width,height,callback;
        //如果是移动端，就使用自适应大小弹窗
        if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {width = 'auto';height = 'auto';}
        if ($.common.isEmpty(title)) {title = false;};
        if ($.common.isEmpty(url)) {url = "/404.html";};
        if ($.common.isEmpty(width)) {width = 800;};
        if ($.common.isEmpty(height)) {height = ($(window).height() - 50);};
        if ($.common.isEmpty(callback)) {
            callback = function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler();
            }
        }
        layer.open({
            type: 2,
            area: [width + 'px', height + 'px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: title,
            content: url,
            btn: ['确定盘点', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: callback,
            cancel: function(index) {
                return true;
            }
        });
    }
    
</script>
</body>
</html>